<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Liverecord extends BaseModel{

    protected $table = 'liverecord';
    public $timestamps = false;

    public function __construct( )
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 添加直播记录
     */
    public function addLiveRecord($data){
        $insert['roomtype'] = $data['roomtype'];
        $insert['uid'] = $data['uid'];
        $insert['showId'] = time();
        $insert['starttime'] = time();
        $insert['endtime'] = 0;
        $insert['entercount'] = 0 ;
        $insert['sign'] = 'y';
        $insert['roomnum'] = $data['roomnum']; // 直播间号
        $insert['title'] = $data['title']; // 直播标题
        $insert['status'] = 1; // 当前的直播状态
        $this->add($insert);
    }

    /**
     * 获取当前正在直播的id
     */
    public function getOnlineShowId($num){
        $showinfo = $this->getOne(['roomnum'=>$num,'status'=>1]);
        return $showinfo['id'];
    }
}